Skip to content

8386591: C2: wrong result because of broken truncation check in CountedLoopConverter::TruncatedIncrement::build - #31502

Draft
eme64 wants to merge 7 commits into
openjdk:masterfrom
eme64:JDK-8386591-char-truncation-broken
Draft

eme64 wants to merge 7 commits into
openjdk:masterfrom
eme64:JDK-8386591-char-truncation-broken

Conversation

@eme64

@eme64 eme64 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

The third bug covered by #31501.

As we have seen in #31395, the CountedLoopConverter::has_truncation_wrap algorithm had some peculiarities, specifically this one:

For char, we recognize & 0x7fff, and not the actual char truncation (& 0xffff). So we don't actually turn (char) truncation into CountedLoops. Actually, this is a bug, see: JDK-8386591.

So here we are. The issue is that we recognized 0x7fff as char truncation, and accordingly only check that we cannot overflow/wrap the range CHAR = [0 .. 0xffff], so if we got a value in range 0x8000 .. 0xffff, that ends up overflowing/wrapping, but since we only checked for overflow outside the CHAR range, we wrongly decide there is no wrap/overflow.


Fix for 0x7fff:

I now allow both masks 0x7fff and 0xffff, for ranges 0..0x7fff and 0..0xffff respectively. I have no idea why we ever had 0x7fff in the first place, because 0xffff is what's used for char cast, and I don't know of any Java language truncation that would produce the 15-bit unsigned truncation of 0x7fff. But still: we used to optimize it, and so I'm keeping it to avoid regressions.


Also addressing signed 8-bit truncation

I also decided to address an issue in the signed truncation at the same time. I suppose I could split that out into an RFE, but it also seems like a similar oversight, also mentioned in #31395:

suspicious: shift == 8 is ((x << 8) >> 8) is signed 24-bit truncation. But the implementation maps to BYTE. This looks like an original "bug", which does not create any real issues, just means we miss optimizations for byte. We should probably match shift == 24 instead, which would be <<24 >>24.

So I slightly fixed/refactored that part too.


Testing

  • I added 3 regression scenarios for the 0x7fff wrap issue.
  • I was able to turn some IR rules positive in TestHasTruncationWrap.java.

This bug was found by the fuzzer from #31501, which I will integrate soon after this issue is fixed, and then we'll have even better coverage going forward.



Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8386591: C2: wrong result because of broken truncation check in CountedLoopConverter::TruncatedIncrement::build (Bug - P3) ⚠️ Issue is already resolved. Consider making this a "backport pull request" by setting the PR title to Backport <hash> with the hash of the original commit. See Backports.

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/31502/head:pull/31502
$ git checkout pull/31502

Update a local copy of the PR:
$ git checkout pull/31502
$ git pull https://git.openjdk.org/jdk.git pull/31502/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 31502

View PR using the GUI difftool:
$ git pr show -t 31502

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/31502.diff

@bridgekeeper

bridgekeeper Bot commented Jun 12, 2026

Copy link
Copy Markdown

👋 Welcome back epeter! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk

openjdk Bot commented Jun 12, 2026

Copy link
Copy Markdown

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk Bot changed the title JDK-8386591 8386591: C2: wrong result because of broken truncation check in CountedLoopConverter::TruncatedIncrement::build Jun 12, 2026
@openjdk

openjdk Bot commented Jun 12, 2026

Copy link
Copy Markdown

@eme64 The following label will be automatically applied to this pull request:

  • hotspot-compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk

openjdk Bot commented Jun 12, 2026

Copy link
Copy Markdown

The total number of required reviews for this PR has been set to 2 based on the presence of this label: hotspot-compiler. This can be overridden with the /reviewers command.

@openjdk

openjdk Bot commented Jun 19, 2026

Copy link
Copy Markdown

@eme64 this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout JDK-8386591-char-truncation-broken
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk Bot added the merge-conflict Pull request has merge conflict with target branch label Jun 19, 2026
@bridgekeeper

bridgekeeper Bot commented Aug 11, 2026

Copy link
Copy Markdown

@eme64 This pull request has been inactive for more than 8 weeks and will be automatically closed if another 8 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot-compiler [email protected] merge-conflict Pull request has merge conflict with target branch

Development

Successfully merging this pull request may close these issues.

1 participant